﻿using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Controls;
using System.Drawing;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace JobShop
{
    public partial class frmProcess : XtraForm
    {
        private BindingList<TcGapReduction> Algorithms;
        public frmProcess()
        {
            InitializeComponent();
            InitializeData();
        }
        //------------------------------------------------------------------------------

        private void InitializeData()
        {
            Algorithms = new BindingList<TcGapReduction>();
            gcSummary.DataSource = Algorithms;
        }
        //------------------------------------------------------------------------------

        private void btnInput_ItemClick(object sender, ItemClickEventArgs e)
        {
            using (OpenFileDialog dlg = new OpenFileDialog())
            {
                dlg.Filter = "Text Files (*.txt)|*.txt|All files(*.*)|*.*";
                dlg.Multiselect = true;

                if (dlg.ShowDialog() != DialogResult.OK)
                    return;

                AddTasks(dlg.FileNames);
            }
        }
        //------------------------------------------------------------------------------

        private void AddTasks(String[] prmInputs)
        {
            foreach (String input in prmInputs)
            {
                if (!File.Exists(input))
                    continue;

                String name = Path.GetFileNameWithoutExtension(input);
                if (Algorithms.Select(iter => iter.Problem.Name).Contains(name))
                    continue;

                TcGapReduction algo = new TcGapReduction(input);
                algo.ProgressChanged += Algorithm_Progressed;
                algo.RunWorkerCompleted += Algorithm_Finished;
                Algorithms.Add(algo);
            }
        }
        //------------------------------------------------------------------------------

        private void frmProcess_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }
        //------------------------------------------------------------------------------

        private void btnExecute_ItemClick(object sender, ItemClickEventArgs e)
        {
            foreach (TcGapReduction algo in Algorithms.Where(iter => iter.Status == TeRunningStatus.Created))
                algo.RunWorkerAsync();
        }
        //------------------------------------------------------------------------------

        private void Algorithm_Progressed(object sender, ProgressChangedEventArgs e)
        {
            if (IsDisposed || !Visible)
                return;

            if (InvokeRequired)
            {
                BeginInvoke((MethodInvoker)delegate() { Algorithm_Progressed(sender, e); });
                return;
            }

            gcSummary.RefreshDataSource();
        }
        //------------------------------------------------------------------------------

        private void Algorithm_Finished(object sender, RunWorkerCompletedEventArgs e)
        {
            if (IsDisposed || !Visible)
                return;

            if (InvokeRequired)
            {
                BeginInvoke((MethodInvoker)delegate() { Algorithm_Finished(sender, e); });
                return;
            }

            gcSummary.RefreshDataSource();
        }
        //------------------------------------------------------------------------------

        private Int32 HitRowHandle(Point prmHitPoint)
        {
            GridHitInfo hitInfo = gvSummary.CalcHitInfo(prmHitPoint);
            return hitInfo.InRowCell ? hitInfo.RowHandle : -1;
        }
        //------------------------------------------------------------------

        private void gvSummary_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Right)
                return;

            Int32 hitRowHandle = HitRowHandle(e.Location);
            if (hitRowHandle >= 0)
            {
                gvSummary.FocusedRowHandle = hitRowHandle;
                pmProcess.ShowPopup(bmProcess, gcSummary.PointToScreen(e.Location));
            }
        }
        //------------------------------------------------------------------

        private void riShowSolution_ButtonPressed(object sender, ButtonPressedEventArgs e)
        {
            TcGapReduction algo = gvSummary.GetFocusedRow() as TcGapReduction;
            if (algo == null)
                return;

            new frmSolution(algo).Show(this);
        }
        //------------------------------------------------------------------------------

        private void riProperties_ButtonPressed(object sender, ButtonPressedEventArgs e)
        {
            TcGapReduction algo = gvSummary.GetFocusedRow() as TcGapReduction;
            if (algo == null)
                return;

            new frmPropertyEditor(algo.Properties).ShowDialog(this);
            gvSummary.RefreshRow(gvSummary.FocusedRowHandle);
        }
        //------------------------------------------------------------------------------

        private void piReset_ItemPress(object sender, ItemClickEventArgs e)
        {
            TcGapReduction algo = gvSummary.GetFocusedRow() as TcGapReduction;
            if (algo == null)
                return;

            algo.Reset();
            gvSummary.RefreshRow(gvSummary.FocusedRowHandle);
        }
        //------------------------------------------------------------------------------

    }
    //------------------------------------------------------------------------------

}
//------------------------------------------------------------------------------